package com.lele.config.security;

import com.lele.pojo.Admin;
import com.lele.pojo.Role;
import com.lele.service.IAdminService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MyUserDetailsService implements UserDetailsService {
    @Resource
    private IAdminService adminService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Admin admin = adminService.getAdminInfo(username);
        if(admin==null){
            return null;
        }
        List<Role> roles = adminService.getAdminWithRoles(admin.getId());
        admin.setRoles(roles);
        return admin;
    }
}
